<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="医院" prop="hpId">
            <el-select v-model="form.hpId" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.mnName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <template>
              <el-radio v-model="form.sex" label="0">男</el-radio>
              <el-radio v-model="form.sex" label="1">女</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="民族" prop="nation">
            <el-select v-model="form.nation" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.nation"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="职称" prop="professionalPost">
            <el-select v-model="form.professionalPost" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.professional_post"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="专家类型" prop="type">
            <el-select v-model="form.type" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.expert_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="名字(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名字(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="生日" prop="birthday">
            <el-date-picker v-model="form.birthday" type="datetime" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="去世日期" prop="mnDateDeath">
            <el-date-picker v-model="form.mnDateDeath" type="datetime" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="特长(蒙)" >
            <el-input v-model="form.mnStrongPoint" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="特长(汉)" >
            <el-input v-model="form.zhStrongPoint" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="简介(蒙)" >
            <el-input v-model="form.mnIntro" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="简介(汉)" >
            <el-input v-model="form.zhIntro" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :inactive-value="0"
              :active-value="1"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" style="width: 370px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="hpName" label="医院"/>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span v-if="scope.row.sex === '1'">女</span>
            <span v-else>男</span>
          </template>
        </el-table-column>
        <el-table-column prop="nation" label="民族">
          <template slot-scope="scope">
            {{ dict.label.nation[scope.row.nation] }}
          </template>
        </el-table-column>
        <el-table-column prop="professionalPost" label="职称">
          <template slot-scope="scope">
            {{ dict.label.professional_post[scope.row.professionalPost] }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="专家类型">
          <template slot-scope="scope">
            {{ dict.label.expert_type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="mnName" label="名称(蒙)"/>
        <el-table-column prop="zhName" label="名称(汉)"/>
        <el-table-column prop="birthday" label="生日"/>
        <el-table-column prop="mnDateDeath" label="去世日期"/>
        <el-table-column prop="zhStrongPoin" label="特长(汉)"/>
        <el-table-column prop="zhIntro" label="简介(汉)"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="修改者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','renownedExpert:edit','renownedExpert:del'])" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudRenownedExpert from '@/api/medicine/renownedExpert'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getNameAndId} from "@/api/medicine/hospital"
import {getByName} from "../../../api/medicine/renownedExpert";

const defaultForm = {
  id: null,
  hpId: null,
  sex: null,
  nation: null,
  professionalPost: null,
  type: null,
  mnName: null,
  zhName: null,
  mnNameCode: null,
  birthday: null,
  mnDateDeath: null,
  mnStrongPoint: null,
  zhStrongPoin: null,
  mnIntro: null,
  zhIntro: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  status: null,
  remark: null
}
export default {
  name: 'RenownedExpert',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['nation', 'professional_post', 'expert_type'],
  cruds() {
    return CRUD({
      title: '知名专家管理',
      url: 'api/renownedExpert',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudRenownedExpert}
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'renownedExpert:add'],
        edit: ['admin', 'renownedExpert:edit'],
        del: ['admin', 'renownedExpert:del']
      },
      input: '',
      rules: {
        hpId: [
          {required: true, message: '请选择医院', trigger: 'change'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'change'}
        ],
        nation: [
          {required: true, message: '请选择民族', trigger: 'change'}
        ],
        professionalPost: [
          {required: true, message: '请选择职称', trigger: 'change'}
        ],
        type: [
          {required: true, message: '请选择专家类型', trigger: 'change'}
        ],
        mnName: [
          {required: true, message: '请输入名称(蒙)', trigger: 'change'}
        ],
        zhName: [
          {required: true, message: '请输入名称(汉)', trigger: 'change'}
        ],
        birthday: [
          {required: true, message: '请选择出生日期', trigger: 'change'}
        ],
        mnDateDeath: [
          {required: true, message: '请选择去世日期', trigger: 'change'}
        ],
        mnStrongPoint: [
          {required: true, message: '请输入特长(蒙)', trigger: 'change'}
        ],
        zhStrongPoint: [
          {required: true, message: '请输入特长(汉)', trigger: 'change'}
        ],
        mnIntro: [
          {required: true, message: '请输入简介(蒙)', trigger: 'change'}
        ],
        zhIntro: [
          {required: true, message: '请输入简介(汉)', trigger: 'change'}
        ]
      },
      options: [],
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudRenownedExpert.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        if (val === 1) {
          data.status = 0;
        } else {
          data.status = 1;
        }
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    search() {
      getByName(encodeURIComponent(this.input)).then(res => {
        this.crud.data = res;
      })
    }
  },
  mounted() {
    getNameAndId().then(res => {
      this.options = res
    })
  }
}
</script>

<style scoped>

</style>
